package org.bagou.xiangs.framework_mvp.view

import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_main_mvp.*
import org.bagou.xiangs.framework_mvp.R
import org.bagou.xiangs.framework_mvp.model.WechatInfo
import org.bagou.xiangs.framework_mvp.presenter.MainMvpContract
import org.bagou.xiangs.framework_mvp.presenter.MainMvpPresenter

@Route(path = "/mvp/activity/MainMvpActivity2")
class MainMvpActivity2 : AppCompatActivity(),
    MainMvpContract.MainMvpView {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_mvp)

        val mainMvpPresenter = MainMvpPresenter()
        mainMvpPresenter.attach(this)

        mainMvpPresenter.getUserInfoPromote()
    }

    override fun isActive(): Boolean {
        return isFinishing || isDestroyed
    }

    override fun context(): Context {
        return this
    }

    override fun userInfoCallback(wechatInfo: WechatInfo?) {
        wechatInfo?.let { info ->
            updateUI(info)
        }
    }
    private fun updateUI (info: WechatInfo) {
        Glide.with(this).load(info.wechatAvatar).into(user_avatar)
        user_name.text = info.wechatName
        user_id.text = info.wechatID

        recycle_list.layoutManager = LinearLayoutManager(this)
        val adapter = MainMvpAdapter()
        recycle_list.adapter = adapter
        adapter.addData(info.list)
    }
}
